package com.gjt.thread.entity;

import com.gjt.thread.Thread.InstanceDigestRunnable;

import javax.xml.bind.DatatypeConverter;
/**
 * Created with Intel IDEA.
 * 使用实列方法代替静态方法回调 回调实列
 *
 * @Auther: gjt
 * @Date: 2024/3/20 18:23
 */
public class InstanceCallDigest {
    private String fileName;
    private byte[] digest;

    public InstanceCallDigest(String fileName){
        this.fileName=fileName;
    }

    // 这里避免在构造函数中启动线程
    public void calculateDigest(){
        // 创建计算摘要线程 并把自己放入 主要就是让线程把digest 属性赋值
        InstanceDigestRunnable cd = new InstanceDigestRunnable(fileName,this);
        Thread t = new Thread(cd);
        t.start();
    }

    public void receiveDigest(byte[] digest){
        this.digest = digest;
        System.out.println(this);
    }

    @Override
    public String toString() {
       String result = fileName + ": ";
       if(digest != null){
           result += DatatypeConverter.printHexBinary(digest);
       }else {
           result += "digest not available";
       }
       return result;
    }
}
